<!-- 出库登记弹框 -->
<template>
  <el-dialog title="出库登记" :loading="loading" v-model="visible" width="50%" append-to-body :before-close="cancel">
    <el-form class="form-column2" ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="耗材编号" prop="goods_id">
        <!-- <el-input v-model="form.goods_id" placeholder="请输入耗材编号" /> -->
        <InputAutocomplete
          v-model="iptVal"
          placeholder="请输入耗材编号"
          search_type="GOODS"
          @onSelect="onSelect"
        />
      </el-form-item>
      <el-form-item label="急诊标识" prop="emergency_flag" v-if="isEmergency">
        <el-radio-group v-model="form.emergency_flag" disabled>
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="耗材批号" prop="batch_no">
        <el-input v-model="form.batch_no" placeholder="请输入耗材批号" />
      </el-form-item>
      <el-form-item label="耗材效期" prop="expiration_date">
        <el-date-picker
          v-model="form.expiration_date"
          placeholder="请输入耗材效期"
          value-format="YYYY-MM-DD"
          type="date"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="耗材数量" prop="qty">
        <el-input v-model="form.qty" placeholder="请输入耗材数量" />
      </el-form-item>
      <el-form-item label="病历号" prop="medical_record_no">
        <el-input v-model="form.medical_record_no" placeholder="请输入病历号" />
      </el-form-item>
      <el-form-item label="床号" prop="bed_no">
        <el-input v-model="form.bed_no" placeholder="请输入床号" />
      </el-form-item>
      <el-form-item label="病人姓名" prop="patient_name">
        <el-input v-model="form.patient_name" placeholder="请输入病人姓名" />
      </el-form-item>
      <el-form-item label="预计手术时间" prop="use_time">
        <el-date-picker
          v-model="form.use_time"
          placeholder="请输入预计手术时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetime"
          style="width: 100%"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="手术情况" prop="situation">
        <el-input v-model="form.situation" placeholder="请输入手术情况" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="AddOutDialog">
import { outOrder } from "@/api/inventory/outbound";
const { proxy } = getCurrentInstance();
const props = defineProps({
  // 是否急诊出库
  isEmergency: {
    type: Boolean,
    default: false
  }
});
const emits = defineEmits(["success"]);

const type = ref("");
const title = ref("");
const visible = defineModel(false);
const loading = ref(false);
const userTypeList = ref([]);
const form = reactive({
  goods_id: "",
  emergency_flag: 1,
  batch_no: "",
  expiration_date: "",
  qty: "",
  medical_record_no: "",
  bed_no: "",
  patient_name: "",
  use_time: "",
  situation: "",
  remark: ""
});
const rules = reactive({
  goods_id: [{ required: true, message: "请输入耗材编号", trigger: "blur" }],
  batch_no: [{ required: true, message: "请输入耗材批号", trigger: "blur" }],
  expiration_date: [{ required: true, message: "请选择耗材效期", trigger: "change" }],
  qty: [{ required: true, message: "请输入耗材数量", trigger: "blur" }]
});
const iptVal = ref("");

function onSelect(val) {
  iptVal.value = form.goods_id = val;
  nextTick(() => {
    proxy.$refs["formRef"].validateField("goods_id");
  });
}

function cancel() {
  clearForm();
  visible.value = false;
}
async function submitForm() {
  proxy.$refs["formRef"].validate(async (valid) => {
    if (!valid) return;
    const emergency_flag = props.isEmergency ? 1 : 0;
    const res = await outOrder({ ...form, emergency_flag });
    if (res.code === 0) {
      proxy.$modal.msgSuccess("出库登记成功");
      emits("success");
      cancel();
      return;
    }
    proxy.$modal.msgError(res.message);
  });
}
function clearForm() {
  proxy.$refs["formRef"].resetFields();
  Object.keys(form).forEach((key) => {
    form[key] = "";
  });
  form.emergency_flag = 1;
  iptVal.value = "";

  nextTick(() => {
    proxy.$refs["formRef"].clearValidate();
  });
}
</script>
